package com.netease.controller;

import com.huaban.analysis.jieba.JiebaSegmenter;
import com.netease.service.impl.WordSegmentLogServiceImpl;
import com.netease.pojo.WordSegmentLog;
import com.netease.utils.JsonResult;
import com.netease.utils.PagedGridResult;
import com.netease.vo.WordSegmentReferenceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @projectName: modeling-dev
 * @package: com.netease.controller
 * @className: WordSegmentController
 * @author: aochong
 * @description: 分词Controller
 * @date: 2023/4/3 10:46
 * @version: 1.0
 */
@Api(value = "分词日志接口", tags = {"分词日志相关接口"})
@RequestMapping("wordSegment")
@RestController
public class WordSegmentLogController {

    private JiebaSegmenter segmenter = new JiebaSegmenter();
    private static final String PATH = "modeling-service/src/main/resources/dicts/user.dict";

    public static final Integer COMMON_PAGE_SIZE = 10;

    @Autowired
    private WordSegmentLogServiceImpl wordSegmentLogService;

    @ApiOperation(value = "分词日志分页查询", notes = "分词日志分页查询", httpMethod = "GET")
    @GetMapping("/list")
    public JsonResult list(
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
            @RequestParam Integer pageSize
    ) {

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult grid = wordSegmentLogService.queryPagedWordSegmentLog(page, pageSize);

        return JsonResult.ok(grid);
    }

    @ApiOperation(value = "分词日志查询", notes = "分词日志查询", httpMethod = "GET")
    @GetMapping("/select")
    public JsonResult select(@RequestParam String fieldCmmnt) {

        WordSegmentLog wordSegmentLog = new WordSegmentLog();
        wordSegmentLog.setFieldCmmnt(fieldCmmnt);

        //判断字段注释必须不能为空
        if (StringUtils.isBlank(wordSegmentLog.getFieldCmmnt())) {
            return JsonResult.errorMsg("字段注释必须不为空！！！");
        }

        //查询该条分词日志是否存在
        boolean isExist = wordSegmentLogService.queryWordSegmentLogIsExists(wordSegmentLog);

        if (!isExist) {
            return JsonResult.errorMsg("该词根在数据库中不存在！！！");
        }

        return JsonResult.ok(wordSegmentLogService.queryWordSegmentLog(wordSegmentLog));
    }

    @ApiOperation(value = "分词参考查询", notes = "分词参考查询，例如：上年度健康体检费用，调用接口返回结果：上:pre,年度:year,健康:hlth,体检:pe,费用:exps", httpMethod = "GET")
    @GetMapping("/wsrSelect")
    public String wsrSelect(@RequestParam String date) {

        if (StringUtils.isBlank(date)) {
            JsonResult.errorMsg("时间参数必须不为空！！！");
        }

        List<WordSegmentReferenceVO> wordSegmentReferenceVOS = wordSegmentLogService.queryWordSgmntRfrnc(date);

        StringBuilder sb = new StringBuilder();

        for (WordSegmentReferenceVO wordSegmentReferenceVO : wordSegmentReferenceVOS) {
            sb.append(wordSegmentReferenceVO.getFieldCmmnt())
                    .append(",")
                    .append(wordSegmentReferenceVO.getWordSgmntRfrnc())
                    .append(",")
                    .append(wordSegmentReferenceVO.getEngName())
                    .append("<BR />");
        }

        return sb.toString();
    }

}
